/**
 * 
 */
package edu.gatech.oad.yuno;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;

/**
 * @author michael
 *
 */
public class Category extends Model implements Comparable<Category> {
	
	public Category(long id) {
		if (Model.context == null) Model.context = YUNoDosApplication.getAppContext();
		this.id = id;
		this.table_name = "categories";
		this.data = new HashMap<String,Object>();
		this.load();
	}
	
	public Category() {
		this(0);
	}
	

	public void validate() throws ValidationException {
		if (getName() == null || getName().equals(""))
			throw new ValidationException("Name cannot be blank.");
	}
	
	/**
	 * Find a Category in the database by id
	 * @param id
	 * @return the Category if found, null if not found
	 */
	public static Category find(long id) {
		if (Model.context == null) Model.context = YUNoDosApplication.getAppContext();
		TodoListOpenHelper helper = new TodoListOpenHelper(Model.context);
		SQLiteDatabase db = helper.getReadableDatabase();
		String cols[] = {"id"};
		
		Cursor c = db.query("categories", cols, "id="+id, null, null, null, null, "1");
		if (c == null || c.getCount() < 1) {
			db.close();
			return null;
		}
		c.moveToFirst();
		long foundId = c.getLong(c.getColumnIndex("id"));
		
		db.close();
		return new Category(foundId);
	}
	
	public static List<Category> findAllByUser(long userId) {
		if (Model.context == null) Model.context = YUNoDosApplication.getAppContext();
		List<Category> list = new ArrayList<Category>();
		TodoListOpenHelper helper = new TodoListOpenHelper(Model.context);
		SQLiteDatabase db = helper.getReadableDatabase();
		String cols[] = {"id"};
		
		Cursor c = db.query("categories", cols, "user_id="+userId + " OR user_id=0", null, null, null, null, null);
		if (c == null || c.getCount() < 1) {
			db.close();
			return list;
		}
		c.moveToFirst();
		list.add(new Category(c.getLong(c.getColumnIndex("id"))));
		while (!c.isLast()) {
			c.moveToNext();
			list.add(new Category(c.getLong(c.getColumnIndex("id"))));
		}
		Collections.sort(list);
		db.close();
		return list;
	}
	/**
	 * 
	 * @return the user
	 */
	public User getUser() {
		long id = getLong("user_id");
		if (id < 1) return null;
		return new User(id);
	}
	
	/**
	 * 
	 * @param user the user to set
	 */
	public void setUser(User user) {
		if (user == null) data.put("user_id", null);
		else data.put("user_id", Long.toString(user.getId()));
	}
	
	/**
	 * 
	 * @param userId the user id to set
	 */
	public void setUser(long userId) {
		data.put("user_id", Long.toString(userId));
	}
	
	/**
	 * @return the name
	 */
	public String getName() {
		return (String) data.get("name");
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		data.put("name", name);
	}

	/**
	 * @return the color
	 */
	public int getColor() {
		int color = getInt("color");
		if (color == -1) color = 0xffffffff;
		return color;
	}

	/**
	 * @param color the color to set
	 */
	public void setColor(int color) {
		data.put("color", Integer.toString(color));
	}

	@Override
	public String toString() {
		return getName();
	}
	
	@Override
	public int compareTo(Category another) {
		return getName().compareTo(another.getName());
	}
}
